﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sealight.Core.Domain;
using Sealight.API.Framework;
using NHibernate.Criterion;
using NHibernate;
using Sealight.Core.Common;

namespace Sealight.Data
{
    public class TopicDataRepository : NHibernateRepository<TopicEntity>, ITopicDataRepository
    {
        #region ITopicDataRepository Members

        public PictureEntity GetPictureById(Guid id)
        {
            PictureEntity data = null;
            try
            {
                data = UnitOfWork.CurrentSession.Get<PictureEntity>(id);

            }
            catch (ADOException ex)
            {
                //log4net

                //must close session on error
                //if (UnitOfWork.CurrentSession != null && UnitOfWork.CurrentSession.IsOpen)
                //    UnitOfWork.CurrentSession.Close();
                //rethrow the exception
                throw;
            }
            return data;
        }

        public PictureEntity SaveOrUpdatePicture(PictureEntity data)
        {           
            try
            {
                UnitOfWork.CurrentSession.SaveOrUpdate(data);
            }
            catch (ADOException ex)
            {
                //log4net

                //must close session on error
                //if (UnitOfWork.CurrentSession != null && UnitOfWork.CurrentSession.IsOpen)
                //    UnitOfWork.CurrentSession.Close();
                //rethrow the exception
                throw;
            }
            return data;
        }

        public void DeletePicture(PictureEntity data)
        {
            try
            {
                 UnitOfWork.CurrentSession.Delete(data);

            }
            catch (ADOException ex)
            {
                //log4net

                //must close session on error
                //if (UnitOfWork.CurrentSession != null && UnitOfWork.CurrentSession.IsOpen)
                //    UnitOfWork.CurrentSession.Close();
                //rethrow the exception
                throw;
            }
        }

        public IList<PictureEntity> GetPicture()
        {
            IList<PictureEntity> data = null;
            try
            {
                data = UnitOfWork.CurrentSession.CreateCriteria(typeof(PictureEntity))
                    .List<PictureEntity>();

            }
            catch (ADOException ex)
            {
                //log4net

                //must close session on error
                //if (UnitOfWork.CurrentSession != null && UnitOfWork.CurrentSession.IsOpen)
                //    UnitOfWork.CurrentSession.Close();
                //rethrow the exception
                throw;
            }
            return data;
        }

        public TopicLocalizedEntity GetTopicLocalizedById(Guid id)
        {
            TopicLocalizedEntity data = null;
            try
            {
               data = UnitOfWork.CurrentSession.Get<TopicLocalizedEntity>(id);

            }
            catch (ADOException ex)
            {
                //log4net

                //must close session on error
                //if (UnitOfWork.CurrentSession != null && UnitOfWork.CurrentSession.IsOpen)
                //    UnitOfWork.CurrentSession.Close();
                //rethrow the exception
                throw;
            }
            return data;
        }

        public TopicLocalizedEntity GetTopicLocalized(Guid topicId, Guid languageId)
        {
            TopicLocalizedEntity data = null;
            try
            {
                data = UnitOfWork.CurrentSession.CreateCriteria<TopicLocalizedEntity>()
                    .Add(Expression.Eq("Topic.TopicID", topicId))
                    .Add(Expression.Eq("Language.LanguageID", languageId))
                    .UniqueResult<TopicLocalizedEntity>();

            }
            catch (ADOException ex)
            {
                //log4net

                //must close session on error
                //if (UnitOfWork.CurrentSession != null && UnitOfWork.CurrentSession.IsOpen)
                //    UnitOfWork.CurrentSession.Close();
                //rethrow the exception
                throw;
            }
            return data;
        }

        public IList<MenuEntity> GetListMenu(MenuSource source, Guid languageId)
        {

            IList<MenuEntity> data = null;
            try
            {
                data = UnitOfWork.CurrentSession.CreateCriteria<TopicLocalizedEntity>()
                    .Add(Expression.Eq("Source", source))
                    .Add(Expression.Eq("Language.LanguageID", languageId))
                    .List<MenuEntity>();

            }
            catch (ADOException ex)
            {
                //log4net

                //must close session on error
                //if (UnitOfWork.CurrentSession != null && UnitOfWork.CurrentSession.IsOpen)
                //    UnitOfWork.CurrentSession.Close();
                //rethrow the exception
                throw;
            }
            return data;
        }

       public TopicLocalizedEntity SaveOrUpdateTopicLocalized(TopicLocalizedEntity data)
        {
            try
            {
                UnitOfWork.CurrentSession.SaveOrUpdate(data);

            }
            catch (ADOException ex)
            {
                //log4net

                //must close session on error
                //if (UnitOfWork.CurrentSession != null && UnitOfWork.CurrentSession.IsOpen)
                //    UnitOfWork.CurrentSession.Close();
                //rethrow the exception
                throw;
            }
            return data;
        }

        public LanguageEntity GetLanguageById(Guid id)
        {
            LanguageEntity results = null;
            try
            {
                results = UnitOfWork.CurrentSession.Get<LanguageEntity>(id);

            }
            catch (ADOException ex)
            {
                //log4net

                //must close session on error
                //if (UnitOfWork.CurrentSession != null && UnitOfWork.CurrentSession.IsOpen)
                //    UnitOfWork.CurrentSession.Close();
                //rethrow the exception
                throw;
            }

            return results;
        } 
        /// <summary>
        /// Get Resource Value With name, langcode Paramaters
        /// </summary>
        /// <param name="name"></param>
        /// <param name="langCode"></param>
        /// <returns></returns>
        public string GetResourceValue(string name, string langCode)
        {
           IList<LocaleStringResourceEntity> results = null;
            try
            {
                results = UnitOfWork.CurrentSession.CreateCriteria(typeof(LocaleStringResourceEntity))
               .Add(Expression.Eq("ResourceName", name))
               .Add(Expression.Eq("Code", langCode)).List<LocaleStringResourceEntity>();
               
            }
            catch (ADOException ex)
            {
                //log4net

                //must close session on error
                //if (UnitOfWork.CurrentSession != null && UnitOfWork.CurrentSession.IsOpen)
                //    UnitOfWork.CurrentSession.Close();
                //rethrow the exception
                throw;
            }

            if (results != null && results.Count > 0)
                return results[0].ResourceValue;

            return string.Empty;
        } 
        /// <summary>
        /// Get Resource Value By langCode
        /// </summary>
        /// <param name="langCode"></param>
        /// <returns></returns>
        public IList<LocaleStringResourceEntity> GetResourceValue(string langCode)
        {
            IList<LocaleStringResourceEntity> results = null;
            try
            {
                results = UnitOfWork.CurrentSession.CreateCriteria(typeof(LocaleStringResourceEntity))               
               .Add(Expression.Eq("Code", langCode)).List<LocaleStringResourceEntity>();

            }
            catch (ADOException ex)
            {
                //log4net

                //must close session on error
                //if (UnitOfWork.CurrentSession != null && UnitOfWork.CurrentSession.IsOpen)
                //    UnitOfWork.CurrentSession.Close();
                //rethrow the exception
                throw;
            }

            return results;
        }
        /// <summary>
        /// Get All Resource Value
        /// </summary>
        /// <returns></returns>
        public IList<LocaleStringResourceEntity> GetResourceValue()
        {
            IList<LocaleStringResourceEntity> results = null;
            try
            {
                results = UnitOfWork.CurrentSession.CreateCriteria(typeof(LocaleStringResourceEntity))
                    .List<LocaleStringResourceEntity>();
            }
            catch (ADOException ex)
            {
                //log4net

                //must close session on error
                //if (UnitOfWork.CurrentSession != null && UnitOfWork.CurrentSession.IsOpen)
                //    UnitOfWork.CurrentSession.Close();
                //rethrow the exception
                throw;
            }

            return results;
        }

        #endregion
    }
}
